﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HA.Data ;
namespace HA.ModFlow.Controls.Windows
{
    /// <summary>
    /// winGridCellV2.xaml 的交互逻辑
    /// </summary>
    public partial class winGridCellV2 : Window
    {
        public winGridCellV2(Project project, List<int> selectedIndices ,HA.ModFlow.Controls.PresentationClass.PCMt3dGridCellProperty mt3dGridCell)
        {
            InitializeComponent();


            _gridCell = new PCGridCell(project, selectedIndices);

            this.DataContext = this._gridCell;

            if (project.MfData == null)
            {
                this.tab1.Visibility = System.Windows.Visibility.Collapsed;

                //this.txtWetDry.Visibility = System.Windows.Visibility.Collapsed;
                //this.txtHK.Visibility = System.Windows.Visibility.Collapsed;
                //this.txtTrans.Visibility = System.Windows.Visibility.Collapsed;
                //this.txtLeak.Visibility = System.Windows.Visibility.Collapsed;
                //this.txtSf1.Visibility = System.Windows.Visibility.Collapsed;
                //this.txtSf2.Visibility = System.Windows.Visibility.Collapsed;
                //this.txtVani.Visibility = System.Windows.Visibility.Collapsed;
                //this.txtHani.Visibility = System.Windows.Visibility.Collapsed;
                //this.txtSs.Visibility = System.Windows.Visibility.Collapsed;
                //this.txtSy.Visibility = System.Windows.Visibility.Collapsed;
                //this.txtTopEle.Visibility = System.Windows.Visibility.Collapsed;
                //this.txtBotEle.Visibility = System.Windows.Visibility.Collapsed;
                //this.txtIBound.Visibility = System.Windows.Visibility.Collapsed;
                //this.txtStartHead.Visibility = System.Windows.Visibility.Collapsed;

                return;
            }

            this.txtWetDry.Visibility = project.MfData.FlowData.CanWet ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
            this.txtHK.Visibility = project.MfData.GridData.LayersCount > 1 ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;

            switch (project.MfData.ModelOption.InnerPack)
            {
                case Domain.DomainLayer.INNERPACK.BCF:

                    //this.txtTrans.Visibility = System.Windows.Visibility.Visible;
                    //this.txtLeak.Visibility = System.Windows.Visibility.Visible;
                    if (!project.MfData.FlowData.IsHaveTransient)
                    {
                        this.txtSf1.Visibility = System.Windows.Visibility.Collapsed;
                        this.txtSf2.Visibility = System.Windows.Visibility.Collapsed;
                    }
                    this.txtVani.Visibility = System.Windows.Visibility.Collapsed;
                    this.txtHani.Visibility = System.Windows.Visibility.Collapsed;
                    this.txtSs.Visibility = System.Windows.Visibility.Collapsed;
                    this.txtSy.Visibility = System.Windows.Visibility.Collapsed;

                    break;

                case Domain.DomainLayer.INNERPACK.LPF:

                    //this.txtVani.Visibility = System.Windows.Visibility.Visible;
                    //this.txtHani.Visibility = System.Windows.Visibility.Visible;
                    if (!project.MfData.FlowData.IsHaveTransient)
                    {
                        this.txtSs.Visibility = System.Windows.Visibility.Collapsed;
                        this.txtSy.Visibility = System.Windows.Visibility.Collapsed;
                    }

                    this.txtTrans.Visibility = System.Windows.Visibility.Collapsed;
                    this.txtLeak.Visibility = System.Windows.Visibility.Collapsed;
                    this.txtSf1.Visibility = System.Windows.Visibility.Collapsed;
                    this.txtSf2.Visibility = System.Windows.Visibility.Collapsed;

                    break;

                default:
                    throw new ArgumentException();
            }


            //MT3D部分          
            //this.mt3dGridCell = mt3dGridCell;
            //this.mt3dGridCell.InitializationData(project, selectedIndices);
            //this.gridMT3D.DataContext = mt3dGridCell;
            ShowData(project);
            this.gridMT3D.DataContext = this.GridCell.MtCell;



            MenuItem itemCopy = new MenuItem();
            itemCopy.Header = "复制";
            itemCopy.Command = ApplicationCommands.Copy;

            //MenuItem itemPaste2 = new MenuItem();
            //itemPaste2.Header = "粘贴";
            //itemPaste2.InputGestureText = "Ctrl+V";
            //itemPaste2.Click += new RoutedEventHandler(itemPaste2_Click);

            ContextMenu cm = new ContextMenu();
            cm.Items.Add(itemCopy);
            //cm.Items.Add(itemPaste2);          
            this.dtGrid.ContextMenu = cm;
        }

        //HA.ModFlow.Controls.PresentationClass.PCMt3dGridCellProperty mt3dGridCell;
       

        PCGridCell _gridCell;

        public PCGridCell GridCell
        {
            get { return _gridCell; }
            set { _gridCell = value; }
        }


        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
          
            this.DialogResult = true;
            this.Close();
        }

        private void btnCancal_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string str = "";
            for (int i = 0; i < this.GridCell.Indices.Count; i++)
            {
                if (i + 1 > 9)
                {
                    str += this.GridCell.Indices[i] + "....";
                    break;
                }
                else
                {
                    str += this.GridCell.Indices[i] + ",";
                }
            }
            str = str.Remove(str.Length - 1, 1);
            this.txtCell.Text = "所选择的单元格:" + str;

            if (this.GridCell.ModflowCell != null)
            {
                this.tabModflow.Visibility = Visibility.Visible;
            }
            else
            {
                this.tabModflow.Visibility = Visibility.Hidden;
            }
            if (this.GridCell.MtCell != null)
            {
                this.tabMT3D.Visibility = Visibility.Visible;
            }
            else
            {
                this.tabMT3D.Visibility = Visibility.Hidden;
            }


        }

        private void ShowData(Project proj)
        {
            if (proj.Mt3dmsData == null)
            {
                return;
            }

            this.txtHtop.IsEnabled = !proj.Mt3dmsData.BasicTransport.HtopEqualsTop;
            this.txtLongDispersivity.IsEnabled = proj.Mt3dmsData.Dispersion.Enabled;

         


            this.dtGrid.AutoGenerateColumns = false;
            this.dtGrid.CanUserAddRows = false;
            this.dtGrid.CanUserReorderColumns = false;
            this.dtGrid.CanUserSortColumns = false;
            DataGridTextColumn txt = new DataGridTextColumn();
            txt.Header = "物种";
            txt.IsReadOnly = true;
            Binding bin = new Binding("Name");
            txt.Binding = bin;
            this.dtGrid.Columns.Add(txt);

            DataGridTextColumn txtStartConc = new DataGridTextColumn();
            txtStartConc.Header = "初始浓度\n[" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetConcentration(proj.Mt3dmsData.BasicTransport.ModelOption.Units) + "]";
            bin = new Binding("StartConc");
            txtStartConc.Binding = bin;
            this.dtGrid.Columns.Add(txtStartConc);

            DataGridTextColumn txtIC = new DataGridTextColumn();
            txtIC.Header = "吸附状态\n初始浓度\n[" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetConcentration(proj.Mt3dmsData.BasicTransport.ModelOption.Units) + "]";
            bin = new Binding("IC");
            txtIC.Binding = bin;
            this.dtGrid.Columns.Add(txtIC);

            DataGridTextColumn txtC1 = new DataGridTextColumn();
            txtC1.Header = "第一吸附参数\n[" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetLength(proj.Mt3dmsData.BasicTransport.ModelOption.Units.Length) + "^3/" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetMass(proj.Mt3dmsData.BasicTransport.ModelOption.Units.Mass) + "]";
           bin = new Binding("Const1");
           txtC1.Binding = bin;
           this.dtGrid.Columns.Add(txtC1);

           DataGridTextColumn txtC2 = new DataGridTextColumn();
           txtC2.Header = "第二吸附或\n双区域模型\n参数[1/" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetTime(proj.Mt3dmsData.BasicTransport.ModelOption.Units.Time) + "]";           
            bin = new Binding("Const2");
            txtC2.Binding = bin;
            this.dtGrid.Columns.Add(txtC2);

            DataGridTextColumn txtR1 = new DataGridTextColumn();
            txtR1.Header = "溶解状态的\n一阶反应率\n[1/" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetTime(proj.Mt3dmsData.BasicTransport.ModelOption.Units.Time) + "]"; 
            bin = new Binding("Rate1");
            txtR1.Binding = bin;
            this.dtGrid.Columns.Add(txtR1);

            DataGridTextColumn txtR2 = new DataGridTextColumn();
            txtR2.Header = "吸附状态的\n一阶反应率\n[1/" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetTime(proj.Mt3dmsData.BasicTransport.ModelOption.Units.Time) + "]"; 
            bin = new Binding("Rate2");
            txtR2.Binding = bin;
            this.dtGrid.Columns.Add(txtR2);

            this.dtGrid.ItemsSource = this.GridCell.MtCell.ArrySpecies;


            switch (proj.Mt3dmsData.ChemicalReaction.IsoTHM)
            {
                case MT3DMS.DomainLayer.ISOTHMType.NOSorption:
                    this.txtBulk.IsEnabled = false;
                    this.txtImmobile.IsEnabled = false;

                    if (proj.Mt3dmsData.ChemicalReaction.KineticRateReaction == MT3DMS.DomainLayer.KineticRateReaction.NONE)
                    {
                        txtIC.IsReadOnly = true;
                        txtC1.IsReadOnly = true;
                        txtC2.IsReadOnly = true;
                        txtR1.IsReadOnly = true;
                        txtR2.IsReadOnly = true;
                    }
                    else
                    {
                        txtIC.IsReadOnly = true;
                        txtC1.IsReadOnly = true;
                        txtC2.IsReadOnly = true;
                    }
                    break;
                case MT3DMS.DomainLayer.ISOTHMType.LinearIsoTherm:
                    this.txtBulk.IsEnabled = true;
                    this.txtImmobile.IsEnabled = false;

                    if (proj.Mt3dmsData.ChemicalReaction.KineticRateReaction == MT3DMS.DomainLayer.KineticRateReaction.NONE)
                    {
                        txtIC.IsReadOnly = true;                    
                        txtC2.IsReadOnly = true;
                        txtR1.IsReadOnly = true;
                        txtR2.IsReadOnly = true;
                    }
                    else
                    {
                        txtIC.IsReadOnly = true;                    
                        txtC2.IsReadOnly = true;
                    }
                    break;
                case MT3DMS.DomainLayer.ISOTHMType.FreundlichIsoTherm:
                    this.txtBulk.IsEnabled = true;
                    this.txtImmobile.IsEnabled = false;

                    if (proj.Mt3dmsData.ChemicalReaction.KineticRateReaction == MT3DMS.DomainLayer.KineticRateReaction.NONE)
                    {
                        txtIC.IsReadOnly = true;
                        txtC2.IsReadOnly = true;
                        txtR1.IsReadOnly = true;
                        txtR2.IsReadOnly = true;
                    }
                    else
                    {
                        txtIC.IsReadOnly = true;                      
                    }
                    break;
                case MT3DMS.DomainLayer.ISOTHMType.LangmuirIsoTherm:
                    this.txtBulk.IsEnabled = true;
                    this.txtImmobile.IsEnabled = false;

                    if (proj.Mt3dmsData.ChemicalReaction.KineticRateReaction == MT3DMS.DomainLayer.KineticRateReaction.NONE)
                    {
                        txtIC.IsReadOnly = true;                      
                        txtR1.IsReadOnly = true;
                        txtR2.IsReadOnly = true;
                    }
                    else
                    {
                        txtIC.IsReadOnly = true;
                    }
                    break;
                case MT3DMS.DomainLayer.ISOTHMType.FirstOrderKineticSorption:
                    this.txtBulk.IsEnabled = true;
                    this.txtImmobile.IsEnabled = false;

                    if (proj.Mt3dmsData.ChemicalReaction.KineticRateReaction == MT3DMS.DomainLayer.KineticRateReaction.NONE)
                    {                       
                        txtR1.IsReadOnly = true;
                        txtR2.IsReadOnly = true;
                    }
                    if (!proj.Mt3dmsData.ChemicalReaction.IsSpecifyIC)
                    {
                        txtIC.IsReadOnly = true;
                    }
                    break;
                case MT3DMS.DomainLayer.ISOTHMType.DualDomainTransferWithoutSorption:
                    this.txtBulk.IsEnabled = false;
                    this.txtImmobile.IsEnabled = true;

                    if (proj.Mt3dmsData.ChemicalReaction.KineticRateReaction == MT3DMS.DomainLayer.KineticRateReaction.NONE)
                    {
                        txtIC.IsReadOnly = true;
                        txtC1.IsReadOnly = true;                      
                        txtR1.IsReadOnly = true;
                        txtR2.IsReadOnly = true;
                    }
                    else
                    {                      
                        txtC1.IsReadOnly = true;                     
                    }
                    if (!proj.Mt3dmsData.ChemicalReaction.IsSpecifyIC)
                    {
                        txtIC.IsReadOnly = true;
                    }
                    break;
                case MT3DMS.DomainLayer.ISOTHMType.DualDomainTransferWithSorption:
                    this.txtBulk.IsEnabled = true;
                    this.txtImmobile.IsEnabled = true;

                    if (proj.Mt3dmsData.ChemicalReaction.KineticRateReaction == MT3DMS.DomainLayer.KineticRateReaction.NONE)
                    {                      
                        txtR1.IsReadOnly = true;
                        txtR2.IsReadOnly = true;
                    }
                    if (!proj.Mt3dmsData.ChemicalReaction.IsSpecifyIC)
                    {
                        txtIC.IsReadOnly = true;
                    }
                    break;
            }
            if (!proj.Mt3dmsData.ChemicalReaction.Enabled)
            {
                this.txtBulk.IsEnabled = false;
                this.txtImmobile.IsEnabled = false;
                txtIC.IsReadOnly = true;
                txtC1.IsReadOnly = true;
                txtC2.IsReadOnly = true;
                txtR1.IsReadOnly = true;
                txtR2.IsReadOnly = true;
            }
        }
    }
}
